﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Configuration;

namespace FortuneCookie.EPiServer.SearchAndReplace.Configuration
{
    /// <summary>
    /// Configuration helper class
    /// </summary>
    internal static class ConfigurationHelper
    {

        #region Members

        private static SearchAndReplaceConfiguration _config;
        private static ReadOnlyCollection<string> _excludedPageTypesNames;
        private static ReadOnlyCollection<string> _excludedPropertyNames;
        private static ReadOnlyCollection<string> _allowedRoles;

        #endregion Members

        #region Properties

        /// <summary>
        /// Gets whether the plugin is enabled
        /// </summary>
        public static bool Enabled
        {
            get
            {
                Initialise();

                return _config.Enabled;
            }
        }

        /// <summary>
        /// Gets whether the search and replaces can be deleted
        /// </summary>
        public static bool AllowDeletionOfSearchAndReplaces
        {
            get
            {
                Initialise();

                return _config.AllowDeletionOfSearchAndReplaces;
            }
        }

        /// <summary>
        /// Gets whether the dynamic properties can be updated
        /// </summary>
        public static bool AllowUpdatingOfDynamicProperties
        {
            get
            {
                Initialise();

                return _config.AllowUpdatingOfDynamicProperties;
            }
        }

        /// <summary>
        /// Gets whether the approve and rollback buttons should be shown if a search is not complete
        /// </summary>
        public static bool DisableApproveAndRollbackButtonsIfSearchNotCompleted
        {
            get
            {
                Initialise();

                return _config.DisableApproveAndRollbackButtonsIfSearchNotCompleted;
            }
        }

        /// <summary>
        /// Gets whether or not markup can be replaced if find text is markup
        /// </summary>
        public static bool AllowMarkupToBeReplacedIfFindTextIsMarkup
        {
            get
            {
                Initialise();

                return _config.AllowMarkupToBeReplacedIfFindTextIsMarkup;
            }
        }

        /// <summary>
        /// Gets the roles that are allowed
        /// </summary>
        public static ReadOnlyCollection<string> AllowedRoles
        {
            get
            {
                Initialise();

                if (_allowedRoles == null)
                    _allowedRoles = _config.AllowedRoles.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList().AsReadOnly();

                return _allowedRoles;
            }
        }

        /// <summary>
        /// Gets the xml file path type
        /// </summary>
        public static XmlFilePathType XmlFilePathType
        {
            get
            {
                Initialise();

                return _config.XmlFilePathType;
            }
        }

        /// <summary>
        /// Gets the xml file path
        /// </summary>
        public static string XmlFilePath
        {
            get
            {
                Initialise();

                return _config.XmlFilePath;
            }
        }

        /// <summary>
        /// Gets the data helper full type name
        /// </summary>
        public static string DataHelperFullTypeName
        {
            get
            {
                Initialise();

                return _config.DataHelperFullTypeName;
            }
        }

        /// <summary>
        /// Gets the connection string name
        /// </summary>
        public static string ConnectionStringName
        {
            get
            {
                Initialise();

                return _config.ConnectionStringName;
            }
        }

        /// <summary>
        /// Gets whether tables and stored procedures should be created if they don't already exist
        /// </summary>
        public static bool CreateTablesAndStoredProcedures
        {
            get
            {
                Initialise();

                return _config.CreateTablesAndStoredProcedures;
            }
        }

        /// <summary>
        /// Gets the excluded page type names
        /// </summary>
        public static ReadOnlyCollection<string> ExcludedPageTypeNames
        {
            get
            {
                Initialise();

                if (_excludedPageTypesNames == null)
                    _excludedPageTypesNames = _config.ExcludedPageTypeNames.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList().AsReadOnly();

                return _excludedPageTypesNames;
                
            }
        }

        /// <summary>
        /// Gets the excluded property names
        /// </summary>
        public static ReadOnlyCollection<string> ExcludedPropertyNames
        {
            get
            {
                Initialise();

                if (_excludedPropertyNames == null)
                    _excludedPropertyNames = _config.ExcludedPropertyNames.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList().AsReadOnly();

                return _excludedPropertyNames;

            }
        }

        /// <summary>
        /// Gets the progress dialog refresh rate
        /// </summary>
        public static int ProgressRefreshRate
        {
            get
            {
                Initialise();

                return _config.ProgressRefreshRate;
            }
        }

        /// <summary>
        /// Gets the thread sleep delay between pages
        /// </summary>
        public static int SleepDelayBetweenPages
        {
            get
            {
                Initialise();

                return _config.SleepDelayBetweenPages;
            }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Initiliases config settings
        /// </summary>
        private static void Initialise()
        {
            if (_config != null)
                return;

            SearchAndReplaceConfiguration config = ConfigurationManager.GetSection("searchAndReplaceConfiguration") as SearchAndReplaceConfiguration;

            if (config == null)
                throw new ConfigurationErrorsException("The SearchAndReplace plugin has not been configured in the application config.");

            _config = config;
        }

        #endregion Methods

    }
}
